实例化
const date = new Date()
const date = new Date("2001-03-02")
日期对象方法
<style>
div{
width: 400px;
height: 40px;
border: 2px solid black;
line-height: 40px;
text-align: center;
}
</style>
<body>
<div>
</div>
<script>
const div = document.querySelector("div")
function getDate(){
const date = new Date()
let hour = date.getHours()
let minute = date.getMinutes()
let second = date.getSeconds()
hour = hour < 10 ? "0" + hour : hour
minute = minute < 10 ? "0" + minute : minute
second = second < 10 ? "0" + second : second
return `今天的时间是:${date.getFullYear()}年${date.getMonth()+1}月${date.getDate()}号 ${hour}:${minute}:${second}`
}
div.innerHTML = getDate()
setInterval(function(){
div.innerHTML = getDate()
},1000)
</script>
</body>
时间戳
时间戳—获取方式
const date = new Date()
console.log(date.getTime())
console.log(+new Date())
console.log(Date.now())
时间戳——常用计算公式
DOM节点
查找节点
子元素.parentNode
<body>
<div class="yeye">
<div class="baba">
<div class="son"></div>
</div>
</div>
<script>
const son = document.querySelector(".son")
// 返回son上一级节点 baba
console.log(son.parentNode)
// 返回son的上一级节点 baba的上一级节点yeye
console.log(son.parentNode.parentNode)
</script>
</body>
下一个兄弟节点
nextElementSibling
上一个兄弟节点
previousElementSibling
增加节点
document.createElement("标签名")
// 插入到父元素的最后
父元素.appendChild(要插入的元素)
// 插入到某个子元素的前面
父元素.insertBefore(要插入的元素,在哪个元素前面)
<body>
<ul>
<li>我是老大</li>
</ul>
<script>
const ul = document.querySelector("ul")
// 1.创建节点
const li = document.createElement("li")
li.innerHTML = "我是li"
// 2.追加节点【作为最后一个元素】
ul.append(li)
// 3.追加节点【作为第一个元素】
const oneLi = document.createElement("li")
oneLi.innerHTML = "我是后加入的老大"
ul.insertBefore(oneLi,ul.children[0])
</script>
</body>
案例
<body>
<!-- 4. box核心内容区域开始 -->
<div class="box w">
<div class="box-hd">
<h3>精品推荐</h3>
<a href="#">查看全部</a>
</div>
<div class="box-bd">
<ul class="clearfix">
</ul>
</div>
</div>
<script>
// 1. 重构
let data = [{
src: 'images/course01.png',
title: 'Think PHP 5.0 博客系统实战项目演练',
num: 1125
},
{
src: 'images/course02.png',
title: 'Android 网络动态图片加载实战',
num: 357
},
{
src: 'images/course03.png',
title: 'Angular2 大前端商城实战项目演练',
num: 22250
},
{
src: 'images/course04.png',
title: 'Android APP 实战项目演练',
num: 389
},
{
src: 'images/course05.png',
title: 'UGUI 源码深度分析案例',
num: 124
},
{
src: 'images/course06.png',
title: 'Kami2首页界面切换效果实战演练',
num: 432
},
{
src: 'images/course07.png',
title: 'UNITY 从入门到精通实战案例',
num: 888
},
{
src: 'images/course08.png',
title: 'Cocos 深度学习你不会错过的实战',
num: 590
},
]
// 增加节点li并渲染数据
const ul = document.querySelector(".box-bd ul")
for (let i = 0; i < data.length; i++){
const li = document.createElement("li")
li.innerHTML = `
<a href="#">
<img src=${data[i].src} alt="">
<h4>
${data[i].title}
</h4>
<div class="info">
<span>高级</span> • <span>${data[i].num}</span>人在学习
</div>
</a>
`
ul.appendChild(li)
}
</script>
</body>
增加节点——克隆节点
const 元素名称 = 元素.cloneNode(布尔值)
删除节点
删除元素必须通过父元素删除:父元素.removeChild(元素)
<body>
<ul>
<li>没用了</li>
</ul>
<script>
const ul = document.querySelector("ul")
// 删除节点
ul.removeChild(ul.children[0])
</script>
</body>
移动端也有自己独特的地方,比如触屏事件
<style>
div {
width: 200px;
height: 200px;
background-color: pink;
}
</style>
<body>
<div></div>
<script>
const div = document.querySelector("div")
div.addEventListener("touchstart",function(){
console.log("触摸事件")
})
div.addEventListener("touchend",function(){
console.log("离开")
})
div.addEventListener("touchmove",function(){
console.log("触摸移动")
})
</script>
</body>
官网
实例演示
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./swiper/swiper-bundle.min.css" />
<style>
.box {
width: 800px;
height: 300px;
margin: 100px auto;
background-color: pink;
}
html,
body {
position: relative;
height: 100%;
}
body {
background: #eee;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color: #000;
margin: 0;
padding: 0;
}
.swiper {
overflow: hidden;
width: 100%;
height: 100%;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
/* Center slide text vertically */
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
}
.swiper-slide img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
</head>
<body>
<div class="box">
<div class="swiper mySwiper">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
<div class="swiper-slide">Slide 4</div>
<div class="swiper-slide">Slide 5</div>
<div class="swiper-slide">Slide 6</div>
<div class="swiper-slide">Slide 7</div>
<div class="swiper-slide">Slide 8</div>
<div class="swiper-slide">Slide 9</div>
</div>
<div class="swiper-pagination"></div>
</div>
</div>
<script src="./swiper/swiper-bundle.min.js"></script>
<script>
var swiper = new Swiper(".mySwiper", {
// 小圆点
pagination: {
el: ".swiper-pagination",
},
// 自动播放
autoplay: {
delay: 1000, //1秒切换一次
disableOnInteraction: false,
},
// 键盘控制
keyboard: {
enabled: true,
onlyInViewport: true,
},
});
</script>
</body>
<body>
<h1>新增学员</h1>
<form class="info" autocomplete="off">
姓名:<input type="text" class="uname" name="uname" />
年龄:<input type="text" class="age" name="age" />
性别:
<select name="gender" class="gender">
<option value="男">男</option>
<option value="女">女</option>
</select>
薪资:<input type="text" class="salary" name="salary" />
就业城市:<select name="city" class="city">
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="广州">广州</option>
<option value="深圳">深圳</option>
<option value="曹县">曹县</option>
</select>
<button class="add">录入</button>
</form>
<h1>就业榜</h1>
<table>
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>薪资</th>
<th>就业城市</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<!--
<tr>
<td>1001</td>
<td>欧阳霸天</td>
<td>19</td>
<td>男</td>
<td>15000</td>
<td>上海</td>
<td>
<a href="javascript:">删除</a>
</td>
</tr>
-->
</tbody>
</table>
<script>
const uname = document.querySelector(".uname")
const age = document.querySelector(".age")
const gender = document.querySelector(".gender")
const salary = document.querySelector(".salary")
const city = document.querySelector(".city")
// 声明空数组用来存储渲染的数据
const data = []
// 1.录入模块
// 1.1表单提交
const info = document.querySelector(".info")
info.addEventListener("submit", function (e) {
// 阻止提交跳转行为
e.preventDefault()
// 验证非空
if (!(uname.value && age.value && gender.value && salary.value && city.value)) {
return alert("内容不能为空")
}
// 创建对象
const stu = {
stuId: data.length + 1,
uname: uname.value,
age: age.value,
gender: gender.value,
salary: salary.value,
city: city.value,
}
data.push(stu)
// 清空表单
info.reset()
// 渲染数据
render()
})
// 渲染函数
const tbody = document.querySelector("tbody")
function render() {
// 清空tbody
tbody.innerHTML = ""
for (let i = 0; i < data.length; i++) {
// 创建节点
const tr = document.createElement("tr")
tr.innerHTML = `
<td>${data[i].stuId}</td>
<td>${data[i].uname}</td>
<td>${data[i].age}</td>
<td>${data[i].gender}</td>
<td>${data[i].salary}</td>
<td>${data[i].city}</td>
<td>
<a href="javascript:">删除</a>
</td>
`
// 添加节点
tbody.appendChild(tr)
}
}
// 删除
tbody.addEventListener("click", function (e) {
if (e.target.tagName = "A") {
this.removeChild(e.target.parentNode.parentNode)
}
})
</script>
</body>